import sys
sys.path.append(sys.path[0] + '/../lib')

from game import *
from server import *
import unittest 

import copy
import socket
import threading
		

class TestSequenceFunctions(unittest.TestCase):
	def setUp(self):
		return
	
	def testserver(self):
		HOST, PORT = "localhost", 9999
		server = InterceptoServer((HOST, PORT))
		
		server_thread = threading.Thread(target=server.serve_forever)
		server_thread.setDaemon(True)
		server_thread.start()
		print "Server loop running in thread:", server_thread.getName()

#		client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		client = RESTClient()
		client.connect((HOST, PORT))
		
		client.send_request('POST', 'game', {'players': 5, 'shuffle': 0})
		response = client.response
		self.assertEqual(200, response['status'])
		
		client.send_request('POST', 'player', {'id': 1})
		response = client.response
		self.assertEqual(200, response['status'])
		
		client.send_request('GET', 'player/cards')
		response = client.response
		self.assertEqual(200, response['status'])
		self.assertEqual(7, len(response['player']['cards']))
		print 'Client:: player 1 cards > ', response['player']['cards']
		
		client.send_request('GET', 'game/players/nb_cards')
		response = client.response
		self.assertEqual(200, response['status'])
		self.assertEqual(5, len(response['game']['players']))
		for p in response['game']['players']:
			self.assertEqual(7, p['nb_cards'])
		
		client.send_request('PUT', 'player/play', {'card': 'A1'})
		
		client.close()
		
		server.server_close()
	
	
if __name__ == '__main__':
	unittest.main()
